/*
 * SnmpResultApi.cpp
 *
 *  Created on: 2016-1-12
 *      Author: root
 */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include"SnmpResultApi.h"
#include"PublicLog.h"

netsnmp_variable_list*SAFE_FREE_VARBIND(netsnmp_variable_list*&var){
	if(var){snmp_free_varbind(var);var=NULL;}
	return var;
}

int OID_COMPARE(SnmpOidInfo oidinfo,netsnmp_variable_list*var,int checkfull){
	if(checkfull==1){
		return snmp_oid_compare(oidinfo.oidName,oidinfo.oidLen,var->name,var->name_length);
	}else{
		return snmp_oidtree_compare(oidinfo.oidName,oidinfo.oidLen,var->name,var->name_length);
	}
	return 0;
}

void HexToMacString(char*str,size_t strlen,u_char*hex,size_t len){
    memset(str,0,strlen);
    if(len<6){
       strcpy(str,"00:00:00:00:00:00");
    }else{
       sprintf(str,"%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX",
               hex[0],hex[1],hex[2],hex[3],hex[4],hex[5]);
    }
}

void HexToIPString(char*str,size_t strlen,u_char*hex,size_t len){
    memset(str,0,strlen);
    sprintf(str,"%hhu.%hhu.%hhu.%hhu",
        hex[0],hex[1],hex[2],hex[3]);
}
#include<arpa/inet.h>
void HexToIPV6String(char*str,size_t strlen,u_char*hex,size_t len){
    memset(str,0,strlen);
	struct in6_addr sin6_addr;
	int i=0;
	while(i<16){
		sin6_addr.s6_addr[i]=hex[i];
		i++;
	}
	inet_ntop(AF_INET6,&sin6_addr,str,strlen);
}
void ShowSnmpVariableOnce(netsnmp_variable_list*varlist,const char*ip){
    netsnmp_variable_list*var=varlist;
    if(var!=NULL){
		char buff[10240]={0};
		snprint_variable(buff,10240,var->name,var->name_length,var);
		INFO("IP:%s %s",ip,buff);
		//var=var->next_variable;
    }
}
void ShowSnmpVariable(netsnmp_variable_list*varlist,const char*ip){
    netsnmp_variable_list*var=varlist;
    while(var!=NULL){
		char buff[10240]={0};
		snprint_variable(buff,10240,var->name,var->name_length,var);
		INFO("IP:%s %s",ip,buff);
		var=var->next_variable;
    }
}
//检查基础表OID -1不匹配 0一次
int CheckTableOid(const netsnmp_variable_list*result,const SnmpOidInfo oidInfo,int ShowErrInfo){
    if(snmp_oidtree_compare(oidInfo.oidName,oidInfo.oidLen,result->name,result->name_length)!=0){
		if(ShowErrInfo){
			char OidName[1024]={0};
			snprint_objid(OidName,sizeof(OidName),result->name,result->name_length);
			WRONG("OID:%s 不属于 表 %s",OidName,oidInfo.strName);
        }
		return -1;
    }
    return 0;
}

int CheckOidType(const netsnmp_variable_list*result,const SnmpOidInfo oidInfo){
    if(result->type!=oidInfo.oidType){
		WRONG("%s Type(%hhu)不匹配 %hhu",oidInfo.strName,oidInfo.oidType,result->type);
		return -1;
    }
    return 0;
}
int ReadCount32FromSnmpResult(const netsnmp_variable_list*result){
	return *(u_long*)result->val.integer;
}
int ReadStringFromSnmpResult(const netsnmp_variable_list*result,char*str,size_t length){
	if(result->val_len>0){
		memcpy(str,result->val.string,result->val_len<(length-1)?result->val_len:(length-1));
	}
	return 0;
}
//-1失败 0 正常 1其他错误
int ReadCount32FromResult(const netsnmp_variable_list*result,const SnmpOidInfo oidInfo,uint32_t&count32,u_char*oidType){
    if(snmp_oidtree_compare(oidInfo.oidName,oidInfo.oidLen,result->name,result->name_length)!=0){
		return -1;
    }
#if 0	
    if(result->type!=oidInfo.oidType){
		if(oidInfo.oidType!=ASN_NULL){
			if((result->type!=SNMP_NOSUCHINSTANCE)&&(result->type!=SNMP_NOSUCHOBJECT))
				WRONG("%s Type(%hhu)不匹配 %hhu",oidInfo.strName,oidInfo.oidType,result->type);
			return 1;
        }
    }
#endif	
    if(oidType!=NULL)*oidType=result->type;
    if((result->type==ASN_COUNTER||result->type==ASN_INTEGER||result->type==ASN_GAUGE||result->type==ASN_TIMETICKS)&&(result->type!=SNMP_NOSUCHINSTANCE)){
		count32=*(u_long*)result->val.integer;
    }else{
		WRONG("%s Type(%hhu)不匹配 %hhu",oidInfo.strName,oidInfo.oidType,result->type);
    }
    return 0;
}
int ReadCount64FromResult(const netsnmp_variable_list*result,const SnmpOidInfo oidInfo,uint64_t&count64){
    if(snmp_oidtree_compare(oidInfo.oidName,oidInfo.oidLen,result->name,result->name_length)!=0){
		return -1;
    }
    if(result->type!=oidInfo.oidType){
		if((result->type!=SNMP_NOSUCHINSTANCE)&&(result->type!=SNMP_NOSUCHOBJECT))
			WRONG("%s Type(%hhu)不匹配 %hhu",oidInfo.strName,oidInfo.oidType,result->type);
		return 1;
    }
    count64=(uint64_t)result->val.counter64->high<<32|result->val.counter64->low;;
    return 0;
}
int ReadIPFromResult(const netsnmp_variable_list*result,const SnmpOidInfo oidInfo,char*IP){
    if(snmp_oidtree_compare(oidInfo.oidName,oidInfo.oidLen,result->name,result->name_length)!=0){
		return -1;
    }
    if(result->type!=oidInfo.oidType){
		if(result->type!=SNMP_NOSUCHINSTANCE)
			WRONG("%s Type(%hhu)不匹配 %hhu",oidInfo.strName,oidInfo.oidType,result->type);
		return 1;
    }
    HexToIPString(IP,16,result->val.string,result->val_len);
    return 0;
}
int ReadIPV6FromResult(const netsnmp_variable_list*result,const SnmpOidInfo oidInfo,char*IP){
    if(snmp_oidtree_compare(oidInfo.oidName,oidInfo.oidLen,result->name,result->name_length)!=0){
		return -1;
    }
    if(result->type!=oidInfo.oidType){
		if(result->type!=SNMP_NOSUCHINSTANCE)
			WRONG("%s Type(%hhu)不匹配 %hhu",oidInfo.strName,oidInfo.oidType,result->type);
		return 1;
    }
    HexToIPV6String(IP,40,result->val.string,result->val_len);
    return 0;
}
int ReadMacFromResult(const netsnmp_variable_list*result,const SnmpOidInfo oidInfo,char*Mac){
    if(snmp_oidtree_compare(oidInfo.oidName,oidInfo.oidLen,result->name,result->name_length)!=0){
		return -1;
    }
    if(result->type!=oidInfo.oidType){
		if(result->type!=SNMP_NOSUCHINSTANCE)
			WRONG("%s Type(%hhu)不匹配 %hhu",oidInfo.strName,oidInfo.oidType,result->type);
		return 1;
    }
    HexToMacString(Mac,18,result->val.string,result->val_len);
    return 0;
}
int ReadStringFromResult(const netsnmp_variable_list*result,const SnmpOidInfo oidInfo,char*str,size_t length){
    if(snmp_oidtree_compare(oidInfo.oidName,oidInfo.oidLen,result->name,result->name_length)!=0){
		return -1;
    }
    if(result->type!=oidInfo.oidType){
		if(result->type!=SNMP_NOSUCHINSTANCE)
			WRONG("%s Type(%hhu)不匹配 %hhu",oidInfo.strName,oidInfo.oidType,result->type);
		return 1;
    }
    if(result->val_len>0){
		size_t RealLength=(length>result->val_len)?result->val_len:length;
		memcpy(str,result->val.string,RealLength);
    }
    return 0;
}
int ReadOidFromResult(const netsnmp_variable_list*result,const SnmpOidInfo oidInfo,char*str,size_t length){
    if(snmp_oidtree_compare(oidInfo.oidName,oidInfo.oidLen,result->name,result->name_length)!=0){
       return -1;
    }
    if(result->type!=oidInfo.oidType){
		if(result->type!=SNMP_NOSUCHINSTANCE)
			WRONG("%s Type(%hhu)不匹配 %hhu",oidInfo.strName,oidInfo.oidType,result->type);
		return 1;
    }
    if(result->val_len>0){
		snprint_objid(str,length,result->val.objid,result->val_len/sizeof(oid));
    }
    return 0;
}
